import 'package:flutter/material.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';

import '../mvp/model/base_multi_select_bean.dart';
import 'base_lv_state.dart';

///
///多选列表基类
///<D> 网络请求返回data里的数据类型
///<T> 列表List中存放的数据类型

abstract class BaseMultiLvState<W extends StatefulWidget,T extends BaseMultiSelectBean>
    extends BaseLvState<W, T> {
  ///是不为多选状态
  @protected
  bool selectedMode = false;

  ///多选按钮状态
  @protected
  final RxBool allSelected = false.obs;

  /// int 数据下标
  /// String List中所放数据类型
  @protected
  Map<int, T>? selectMap;
  @protected
  late StateSetter setSelectDescRtState;

  @override
  Widget onBottomViewCreate() {
    if (selectedMode) {
      return Container(
          height: 50,
          width: double.infinity,
          color: Colors.grey[100],
          child: Stack(
            alignment: AlignmentDirectional.centerStart,
            children: [
              Obx(() => Checkbox(
                  value: allSelected.value,
                  onChanged: (value) {
                    selectedAll();
                    setSelectDescRtState(() {});
                  })),
              Positioned(
                  left: 40,
                  child: StatefulBuilder(builder:
                      (BuildContext context, void Function(void Function()) setState) {
                    setSelectDescRtState = setState;
                    return RichText(
                      text: TextSpan(
                          text: "全选",
                          style: const TextStyle(color: Colors.black),
                          children: [
                            const TextSpan(
                              text: "   已选择",
                              style: TextStyle(color: Colors.grey, fontSize: 12),
                            ),
                            TextSpan(
                              text: (selectMap != null) ? selectMap?.length.toString() : "0",
                              style: const TextStyle(color: Colors.blue, fontSize: 12),
                            ),
                            const TextSpan(
                              text: "条",
                              style: TextStyle(color: Colors.grey, fontSize: 12),
                            )
                          ]),
                    );
                  }))
            ],
          ));
    } else {
      return super.onBottomViewCreate();
    }
  }

  @override
  void onPopCalled(bool didPop) {
    if (selectedMode) {
      restoreSelectedMode();
    }
  }

  @override
  bool onScopeCanPop() {
    return !selectedMode;
  }

  void restoreSelectedMode() {
    selectedMode = !selectedMode;
    if (selectedMode) {
      selectMap = <int, T>{};
      for (int index = 0; index < adapter.getItemCount(); index++) {
        adapter.ts?[index].visible = true;
      }
    } else {
      selectMap?.clear();
      allSelected.value = false;
      for (int index = 0; index < adapter.getItemCount(); index++) {
        adapter.ts?[index].visible = false;
        adapter.ts?[index].selected = false;
      }
    }
    if(mounted){
      setState(() {});
    }
  }

  /// 选择全部
  void selectedAll() {
    allSelected.value = !allSelected.value;
    if (allSelected.value) {
      selectMap?.clear();
      for (int index = 0; index < adapter.getItemCount(); index++) {
        adapter.ts?[index].selected = true;
        selectMap?.addEntries([MapEntry(index, adapter.ts![index])]);
      }
    } else {
      selectMap?.clear();
      for (int index = 0; index < adapter.getItemCount(); index++) {
        adapter.ts?[index].selected = false;
      }
    }
    if(mounted){
      setState(() {});
    }
  }
}
